import 'package:flutter/services.dart';
import 'channel_error_handler.dart';

/// 页面交互Channel - 负责页面跳转和导航
/// 处理原生页面跳转、页面间数据传递等UI交互
class NavigationChannel {
  static const MethodChannel _channel = MethodChannel('navigation_channel');

  /// 打开页面A，传入用户信息
  static Future<bool> openPageA(Map<String, dynamic> userInfo) async {
    try {
      final result = await _channel.invokeMethod('openPageA', userInfo);
      return result ?? false;
    } on PlatformException catch (e) {
      ChannelErrorHandler.handleError(e, '打开页面A', 'NavigationChannel');
      rethrow;
    }
  }

  /// 打开页面B，传入参数
  static Future<bool> openPageB(Map<String, dynamic> params) async {
    try {
      final result = await _channel.invokeMethod('openPageB', params);
      return result ?? false;
    } on PlatformException catch (e) {
      ChannelErrorHandler.handleError(e, '打开页面B', 'NavigationChannel');
      rethrow;
    }
  }

  /// 打开WebView页面
  static Future<bool> openWebView(String url, {String? title}) async {
    try {
      final params = {'url': url, 'title': title ?? '网页'};
      final result = await _channel.invokeMethod('openWebView', params);
      return result ?? false;
    } on PlatformException catch (e) {
      ChannelErrorHandler.handleError(e, '打开WebView', 'NavigationChannel');
      rethrow;
    }
  }

  /// 打开相机
  static Future<Map<String, dynamic>?> openCamera() async {
    try {
      final result = await _channel.invokeMethod('openCamera');
      final response = result != null ? Map<String, dynamic>.from(result) : null;
      return response;
    } on PlatformException catch (e) {
      ChannelErrorHandler.handleError(e, '打开相机', 'NavigationChannel');
      rethrow;
    }
  }

  /// 打开相册
  static Future<Map<String, dynamic>?> openGallery() async {
    try {
      final result = await _channel.invokeMethod('openGallery');
      final response = result != null ? Map<String, dynamic>.from(result) : null;
      return response;
    } on PlatformException catch (e) {
      ChannelErrorHandler.handleError(e, '打开相册', 'NavigationChannel');
      rethrow;
    }
  }

  /// 显示原生弹窗
  static Future<bool> showNativeDialog(String title, String message, {List<String>? actions}) async {
    try {
      final params = {
        'title': title,
        'message': message,
        'actions': actions ?? ['确定'],
      };
      final result = await _channel.invokeMethod('showNativeDialog', params);
      return result ?? false;
    } on PlatformException catch (e) {
      ChannelErrorHandler.handleError(e, '显示原生弹窗', 'NavigationChannel');
      rethrow;
    }
  }

  /// 关闭当前页面
  static Future<bool> closeCurrentPage() async {
    try {
      final result = await _channel.invokeMethod('closeCurrentPage');
      return result ?? false;
    } on PlatformException catch (e) {
      ChannelErrorHandler.handleError(e, '关闭当前页面', 'NavigationChannel');
      rethrow;
    }
  }
}
